package com.stylefeng.guns.core.aop;

import com.stylefeng.guns.core.common.constant.Const;
import com.stylefeng.guns.core.shiro.ShiroKit;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;

/**
 * 描述:注解解析器
 * 创建人: xiang
 * 日期: 2018/8/9
 * 时间: 17:00
 */
public class PlatformArgumentResolver implements HandlerMethodArgumentResolver {
    @Override
    public boolean supportsParameter(MethodParameter methodParameter) {
        if (methodParameter.hasParameterAnnotation(Platform.class)) {
            return true;
        }
        return false;
    }

    @Override
    public String resolveArgument(MethodParameter methodParameter, ModelAndViewContainer modelAndViewContainer, NativeWebRequest nativeWebRequest, WebDataBinderFactory webDataBinderFactory) throws Exception {
        //1.如果不是总平台 设置setPlatform 当前平台
        //2.如果有getPlatform 值为null 当前平台
       String platform=nativeWebRequest.getParameter(methodParameter.getParameterName());
        if(!Const.DEFAULT_PH.equals(ShiroKit.getUser().getDeptcode()) || StringUtils.isBlank(platform)){
            return ShiroKit.getUser().getDeptcode();
        }
        return  platform;
    }
}
